<script type="text/javascript">
$(function(){
	var pg = App.base.serverEdit.ui, pp = pg.params || {},
		envs = Cache.base.env(App.code);

	var frm = {
		code: pg.seek('code'),
		type: pg.seek('type'),
		name: pg.seek('name'),
		group: pg.seek('group'),
		ip: pg.seek('ip'),
		env: pg.seek('env'),
		memo: pg.seek('memo')
	};
	frm.type.mac('combo', {
		width: 300,
		boxWidth: 301,
		data: Cache.base.server.type
	}).val(0);
	frm.env.mac('combo', {
		boxLeft: 300,
		width: 300,
		boxWidth: 301,
		keyField: 'k',
		displayField: 'v',
		data: envs
	}).val(pp.env);

	function getValues(){
		var pid = pg.params.parent;
		return {
			id: pg.params.id,
			projId: pg.params.projId, 
			code: frm.code.val(),
			name: frm.name.val(),
			type: frm.type.val(),
			group: frm.group.val(),
			ip: frm.ip.val(),
			env: frm.env.val(),
			memo: frm.memo.val()
		};
	}
	function setValues(o, pom){
		frm.code.val(o.code);
		frm.name.val(o.name);
		frm.group.val(o.group);
		frm.ip.val(o.ip);
		frm.env.val(o.env);
		frm.memo.val(o.memo);
		frm.type.val(o.type);
	}
	function reset(){
		frm.code.val('');
		frm.name.val('');
		frm.group.val('');
		frm.ip.val('');
		frm.env.val(0);
		frm.memo.val('');
		frm.type.val('');
		alert('添加成功，继续添加或退出');
	}
	function validate(p){
		var m = pg.seek('msg').html(''),
			x = App.validate, v = App.base.validation, b;
		return x(v.svc.code, p.code, m, '编码') && x(v.svc.name, p.name, m, '名字')
			&& x(v.svc.ip, p.ip, m, 'IP');
	}
	pg.find('input').blur(function(){
		var p = getValues();
		if (!validate(p))
			return;
	});
	pg.seek('ok').click(function(){
		var p = getValues();
		if (!validate(p))
			return;
		$.post(App.ctx + '/server/save.do', p, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				pg.callback();
				if (pp.id) {
					pg.close();
				} else {
					reset();
				}
			} else {
				pg.seek('msg').html(mac.msg(ro.data, ro.params));
			}
		});
		return false;
	});
	setValues(pp);
	
	pg.seek('close').click(pg.close);

	if (pp.id) {
		var w = mac.wait('Loading');
		$.get(App.ctx + '/server/get.do', {
			id: pg.params.id
		}, function(rs) {
			var ro = mac.eval(rs);
			if (ro.success) {
				setValues(ro.data);
			} else {
				pg.seek('msg').html(mac.msg(ro.data, ro.params));
			}
			w.close();
		});
	} else {
		setValues(pp);
	}
});
</script>
<div class="form">
	<div class="item">
		<span class="label">编码：</span> <input type="text" name="code"
			placeholder="请填入服务器的英文名字" /> <span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">名字：</span> <input type="text" name="name"
			placeholder="请填入服务器的中文名字" /> <span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">服务器类型：</span>
		<div class="combo" name="type"></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">Ip：</span> <input type="text" name="ip"
			placeholder="请填入服务器的IP地址" /> <span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">组别：</span> <input type="text" name="group"
			placeholder="请填入服务器的组别" />
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">环境：</span>
		<div class="combo" name="env"></div>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">备忘录：</span>
		<textarea name="memo" placeholder="请填入服务器的memo"></textarea>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">&nbsp;</span>
		<div name="msg" class="msg"></div>
		<div class="clear"></div>
	</div>
	<div class="bottom buttonTop">
		<button name="ok" class="addBtn">确定</button>
		<button name="close" class="addBtn">关闭</button>
	</div>
</div>